package Thread;

public class Demo6 {
    private static final long COUNT = 20_0000_0000;

    private static void serial() {
        long begin = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < COUNT; i++) {
            a++;
        }
        a = 0;
        for (long i = 0; i < COUNT; i++) {
            a++;
        }
        long end = System.currentTimeMillis();
        System.out.println("单线程消耗的时间：" + (end - begin) + "ms");
    }

    private static void concurrency() {
        long begin = System.currentTimeMillis();

        Thread t1 = new Thread(() -> {
            int a = 0;
            for (long i = 0; i < COUNT; i++) {
                a++;
            }
        });

        Thread t2 = new Thread(() -> {
            int a = 0;
            for (long i = 0; i < COUNT; i++) {
                a++;
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join(); // 等待线程结束（等待线程把自己的run方法执行完毕）
                       // 在主线程中调用 t1.join,意思就是让 main线程等待 t1执行完
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("并发执行的时间：" + (end - begin) + "ms");
    }

    public static void main(String[] args) {
        //serial();
        concurrency();
    }
}
